using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;
using Microsoft.DirectX.DirectInput;
using System.Windows.Forms;
using TowerDefense_Dx.Core;
using TowerDefense_Dx.GameLogic;

namespace TowerDefense_Dx.Button
{
    class TowerButton:DxButton
    {
        int towerCost = 10;
        bool buyTower = false;
        public TowerButton(string imagePath, Microsoft.DirectX.DirectDraw.Device graphicDevice, int frameWidth, int frameHeight, PointF position):
            base(imagePath,graphicDevice,frameWidth,frameHeight,position)
        {
            _position.X = position.X;
            _position.Y = position.Y;
            _state = BUTTON_STATE.NORMAL;
            _bounding = new RectangleF(_position.X, _position.Y, _frameWidth, _frameHeight);
          
        }
        public void Update(Microsoft.DirectX.DirectInput.MouseState state, DxInitGraphics graphic, Player player, int[,] colisionMap)
        {
             _buttons = state.GetMouseButtons();
            {
                if (_buttons[0] != 0)
                {
                    if (_bounding.Contains(Cursor.Position))
                    {
                        if (_state == BUTTON_STATE.NORMAL)
                        {
                            _state = BUTTON_STATE.HOLD;
                            OnMouseClick(player);
                            if (_onMouseDown != null)
                            {
                                _onMouseDown();
                            }
                        }
                        else
                        {
                            if (_state == BUTTON_STATE.HOLD)
                            {                                
                                if (_onMouseDrag != null)
                                {
                                    _onMouseDrag();
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (_state == BUTTON_STATE.HOLD)
                    {
                        _state = BUTTON_STATE.NORMAL;
                        OnMouuseUp(player,colisionMap);                        
                        if (_onMouseUp != null)
                        {
                            _onMouseUp();
                        }
                    }
                }
            }            
        }
        public void DrawSprite(Surface destSurface)
        {
            if (sprite != null)
            {
                sprite.DrawFast(Cursor.Position.X - 16, Cursor.Position.Y - 16, 1, destSurface, DrawFastFlags.Wait);
            }
        }
        private void OnMouseClick(Player player)
        {
            if (player.Money >= towerCost)
            {
                
                buyTower = true;
                sprite = new DxSprite("Sprite/sprite.png", _graphicDevice, 32, 32);
            }                            
        }
        private void OnMouuseUp(Player player, int[,] colisionMap)
        {
            if (buyTower == true)
            {
                buyTower = false;
                sprite = null;
                if(CheckColision(Cursor.Position,colisionMap))
                {
                    player.Money -= towerCost;
                    player.AddTower(new PointF((float)((int)Cursor.Position.X / 32 * 32), (float)((int)Cursor.Position.Y / 32 * 32)));
                }
            }
        }
        private bool CheckColision(PointF cursor, int[,] colisionMap)
        {
            int x = ((int)cursor.X - 3) / 32;
            int y = ((int)cursor.Y - 96) / 32;
            bool isAddTower = false;
            if (cursor.X >= 3 && cursor.X <= 515 && cursor.Y >= 96 && cursor.Y <= 480)
            {
                if (colisionMap[y,x] == 2)
                {
                    isAddTower = true;
                }
            }
            else
            {
                isAddTower = false;
            }
            return isAddTower;
        }
    }
}
